import config from "./WebIMConfig";
import websdk from "easemob-websdk";

function ack(message) {
    let bodyId = message.id; // 需要发送已读回执的消息id
    let msg = new WebIM.message("read", WebIM.conn.getUniqueId());
    msg.set({
        id: bodyId,
        to: message.from
    });
    WebIM.conn.send(msg.body);
}

// 初始化IM SDK
let WebIM = {};
WebIM = window.WebIM = websdk;
WebIM.config = config;

WebIM.conn = new WebIM.connection({

    apiUrl: WebIM.config.apiURL,
    appKey: WebIM.config.appKey,
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions,


    heartBeatWait: WebIM.config.heartBeatWait,
    autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
    autoReconnectInterval: WebIM.config.autoReconnectInterval,
    isStropheLog: WebIM.config.isStropheLog,
    delivery: WebIM.config.delivery,
    //公有云 isHttpDNS 默认配置为true
    isHttpDNS: WebIM.config.isHttpDNS,
});


WebIM.conn.listen({

    onOpened: function (message) {          //连接成功回调
        // 如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
        // 手动上线指的是调用conn.setPresence(); 如果conn初始化时已将isAutoLogin设置为true
        // 则无需调用conn.setPresence();

        console.log("恭喜连接成功!!!!")
        // 登录或注册成功后 跳转到好友页面
        const username = window.Vue.$store.state.login.username;
        //const password = Vue.$store.state.login.password;
        //const token = Vue.$store.state.login.token;

        window.Vue.$router.push("/liaotian?username=" + username);
    },
    onClosed: function (message) {
    },         //连接关闭回调
    onTextMessage: function (message) {
        debugger
        console.log("onTextMessage  ------  ",message);
    },    //收到文本消息
    onEmojiMessage: function (message) {
        console.log(message)
    },   //收到表情消息
    onPictureMessage: function (message) {
        console.log(message)
    }, //收到图片消息
    onCmdMessage: function (message) {
        console.log(message)
    },     //收到命令消息
    onAudioMessage: function (message) {
        console.log(message)
    },   //收到音频消息
    onLocationMessage: function (message) {
        console.log(message)
    },//收到位置消息
    onFileMessage: function (message) {
        console.log(message)
    },    //收到文件消息
    onVideoMessage: function (message) {
        var node = document.getElementById('privateVideo');
        var option = {
            url: message.url,
            headers: {
                'Accept': 'audio/mp4'
            },
            onFileDownloadComplete: function (response) {
                var objectURL = WebIM.utils.parseDownloadResponse.call(WebIM.conn, response);
                node.src = objectURL;
            },
            onFileDownloadError: function () {
                console.log('File down load error.')
            }
        };
        WebIM.utils.download.call(WebIM.conn, option);
    },   //收到视频消息
    onPresence: function (message) {
        console.log(message)
    },       //收到联系人订阅请求、处理群组、聊天室被踢解散等消息
    onRoster: function (message) {
        console.log(message)
    },         //处理好友申请
    onInviteMessage: function (message) {
        console.log(message)
    },  //处理群组邀请
    onOnline: function () {

    },                  //本机网络连接成功
    onOffline: function () {

    },                 //本机网络掉线
    onError: function (message) {
        console.log("onError",message)
        //let token= WebIM.utils.getCookie()['webim_'+window.Vue.$store.state.login.username]

    },          //失败回调
    onBlacklistUpdate: function (list) {       //黑名单变动
        // 查询黑名单，将好友拉黑，将好友从黑名单移除都会回调这个函数，list则是黑名单现有的所有好友信息
        console.log(list);
    }
});

export default WebIM;
